package com.slg.android.manyanime.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.bean.MyComment;
import com.slg.android.manyanime.utils.DataStore;

import java.util.List;

/**
 * Created by Darker on 2015/3/12.
 */
public class MyCommentAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<MyComment> comments;

    /**
     * 用于处理购物车项目的选中与未选中的状态
     * 进行总金额的计算
     */
    private View.OnClickListener operationListener;

    public MyCommentAdapter(Context context, List<MyComment> data) {
        if(context != null){
            inflater = LayoutInflater.from(context);
        }else{
            throw new IllegalArgumentException("context must not null");
        }
        comments = data;
    }

    @Override
    public int getCount() {
        int ret = 0 ;
        if(comments !=  null){
            ret = comments.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if(comments != null){
            ret = comments.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setOperationListener(View.OnClickListener operationListener){
        this.operationListener = operationListener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        if(convertView != null){
            ret = convertView;
        }else{
            ret = inflater.inflate(R.layout.item_activity_my_comment_list,parent,false);
        }
        ViewHolder holder = (ViewHolder) ret.getTag();
        if(holder == null){
            holder = new ViewHolder();
            holder.aname = (TextView)ret.findViewById(R.id.item_my_comment_picture_name);
            holder.comment_count = (TextView)ret.findViewById(R.id.item_my_comment_picture_count);
            holder.content = (TextView)ret.findViewById(R.id.item_my_comment_content);
            holder.time = (TextView)ret.findViewById(R.id.item_my_comment_time);
            holder.zan_count = (TextView)ret.findViewById(R.id.item_my_comment_zan_count);
            holder.zan_picture = (ImageView)ret.findViewById(R.id.item_my_comment_zan_hand);
            if(operationListener != null){
                holder.zan_picture.setOnClickListener(operationListener);
            }
            holder.comment_picture = (ImageView)ret.findViewById(R.id.item_my_comment_picture_comment);
            if(operationListener != null){
                holder.comment_picture.setOnClickListener(operationListener);
            }
            holder.picture = (ImageView)ret.findViewById(R.id.item_my_comment_img);
            if(operationListener != null){
                holder.picture.setOnClickListener(operationListener);
            }
            //设置子控件的点击查找
            holder.picture.setTag(position);
            holder.zan_picture.setTag(position);
            holder.comment_picture.setTag(position);
            holder.zan_count.setTag(position);
            holder.comment_count.setTag(position);

            //设置数据
            MyComment comment = comments.get(position);
            holder.time.setText(comment.getCreate_time());
            holder.content.setText(comment.getContent());
            holder.comment_count.setText(comment.getReply_num());
            holder.zan_count.setText(comment.getGood_num());
            holder.aname.setText(comment.getMname());

            BitmapUtils bitmapUtils = DataStore.getBitmapUtils();
            bitmapUtils.configDefaultLoadingImage(R.drawable.item_default);
            bitmapUtils.display(holder.picture,comment.getMpic());

            ret.setTag(holder);

        }
        return ret;
    }

    private static class ViewHolder{
        public TextView content;
        public TextView aname;
        public TextView zan_count;
        public TextView comment_count;
        public ImageView picture;
        public ImageView zan_picture;
        public ImageView comment_picture;
        public TextView time;

    }
}
